package com.njwb.day15.zqzb.ios.io01;

import java.io.*;

public class FileInputStreamDemo02 {
    public static void main(String[] args) {
        //创建一个字节流对象
        InputStream inputStream = null ;
        try {
            File file = new File("e:"+File.separator+"hello.txt");
            //1、构建流
            inputStream = new FileInputStream(file);
            //读取的次数
            int count  = 0 ;
            //通过字节数组进行读取
            byte[] buffer =new byte[3];
            int length = 0 ;
            while((length=inputStream.read(buffer))!=-1) {
                String str = new String(buffer,0,length);
                System.out.print(str);
                count++ ;
            }
            System.out.println("\n使用read(byte[]),一共读取的次数是："+count);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //3、关闭流
            if(inputStream!=null) {
                //3、关闭流
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
